import { Controller, Get, Header, Res } from '@nestjs/common';
import { LargeFileDownloadService } from './large-file-download.service';
import fs from 'node:fs';
import { join } from 'node:path';

@Controller('large-file-download')
export class LargeFileDownloadController {
  constructor(
    private readonly largeFileDownloadService: LargeFileDownloadService,
  ) {}

  /**
   * 下载文件 
   *  window.open('http://127.0.0.1:3333/api/large-file-download/download', '_blank')
   * @param res 
   */
  @Get('download')
  @Header('Content-Type', 'text/json')
  @Header('Content-Disposition', 'attachment; filename=package.json')
  downloadFile(@Res() res: import('express').Response) {
    const pathPackage = join(__dirname, '../../package.json');
    const content = fs.readFileSync(pathPackage, 'utf8');
    // 告诉浏览器应该以附件的形式下载文件，而不是直接显示在浏览器中
    // res.setHeader('Content-Disposition', 'attachment; filename=package.json');
    res.end(content);
  }
}
